package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import android.widget.ImageView;
import android.widget.TextView;
import com.xgkj.diyiketang.data.MessageInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.R;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by windo on 2015/12/18.
 */
public class ZhuBoGongGaoAdapter extends BaseAdapter {

    private Context mContext;

    private List<MessageInfo> mList = new ArrayList<MessageInfo>();

    public ZhuBoGongGaoAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<MessageInfo> list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(mList !=null){
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(mList != null){
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ChildHolder childHolder = null;
        if(convertView == null || convertView.getTag() == null){
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.zhubo_gonggao_item,null);
            childHolder.headImg = (ImageView) convertView.findViewById(R.id.head_img);
            childHolder.nickNameText = (TextView) convertView.findViewById(R.id.nickname);
            childHolder.msgTitle = (TextView) convertView.findViewById(R.id.gonggao_title);
            childHolder.msgTime = (TextView) convertView.findViewById(R.id.gonggao_time);
            convertView.setTag(childHolder);
        }else{
            childHolder = (ChildHolder) convertView.getTag();
        }

        final MessageInfo  fansInfo = mList.get(position);
        if(fansInfo != null){
            String headImgUrl = fansInfo.getUserLogo();
            if(headImgUrl != null && headImgUrl.length()>0){
                ImageCache.getInstance(mContext).loadBitmapToView(childHolder.headImg,headImgUrl, ViewUtil.getHeadImageSize_40(mContext));
//                childHolder.headImg.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        Intent intent = new Intent(mContext, com.xgkj.diyiketang.activity.UserDetailActivity.class);
//                        intent.putExtra("userId",fansInfo.getUserId());
//                        mContext.startActivity(intent);
//                    }
//                });
            }else{
                childHolder.headImg.setImageResource(R.drawable.default_head_image);
            }
            if(fansInfo.getNickName()!=null){
                try {
                    childHolder.nickNameText.setText(URLDecoder.decode(fansInfo.getNickName(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            childHolder.msgTitle.setText(fansInfo.getMsgTitle());
            childHolder.msgTime.setText(fansInfo.getMsgTime());

        }

        return convertView;
    }

    class ChildHolder{
        ImageView headImg;
        TextView nickNameText;
        TextView msgTitle;
        TextView msgTime;
    }
}
